<template>
    <el-card class="box-card">
      <el-form :inline="true" :model="searchData" class="demo-form-inline">
      <el-form-item label="文章标题">
        <el-input v-model="searchData.name" placeholder="请输入名字" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
        <el-button type="success" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" style="width: 100%">
      <el-table-column prop="id" label="id"/>
      <el-table-column prop="userId" label="userId"/>
      <el-table-column prop="title" label="title" />
      <el-table-column prop="body" label="body" />
    </el-table>
    <el-pagination 
    class='page' 
    small 
    layout="prev, pager, next" 
    @current-change="currentChange"
    :total="page.total" />
    </el-card>
  </template>
  
  <script setup lang='ts'>
  import { ref, reactive, onMounted } from 'vue'
  import {testApi} from "@/api/user.ts"
import { computed } from '@vue/reactivity';
  let data = reactive({
      tableData:[],
      allData:[]
  })
  const  searchData = reactive({
    name:"",
    desc:""
  })
  let isSearch = ref(false)
  const page = reactive({
    pageNumber:1,
    pageSize:10,
    total :0
  })

  let dataList = computed(()=>{
    let start = (page.pageNumber-1) * page.pageSize
    let end = page.pageNumber* page.pageSize > page.total ? page.total : page.pageNumber* page.pageSize
    return data.tableData.filter((item,index)=>{
        return index>=start  && index <end
    })
  })
  const currentChange = (number)=>{
    page.pageNumber = number
  }
  onMounted(()=>{
    testApi().then(res=>{
        data.tableData = [...res]
        data.allData = [...res]
        page.total =  data.tableData.length
    })
  })

  const onSubmit = ()=>{
    page.pageNumber = 1
    // isSearch= true
    data.tableData =  data.allData.filter((item,index)=>{
        return item.title.indexOf(searchData.name)!=-1 
    })
    page.total =  data.tableData.length
  }

  const reset = ()=>{
    page.pageNumber = 1
    // isSearch= true
    data.tableData = [...data.allData]
    page.total =  data.tableData.length
    searchData.name = ""
  }
  </script>
  <style lang='scss'  scoped>
  .page{
    display: flex;
    justify-content: center;
    margin:20px auto;
    text-align: center;
  }
  </style>